package com.nbjtjc.safe.api.emg;

import com.nbjtjc.safe.annotation.SysLog;
import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.model.emg.EmgPlanPublic;
import com.nbjtjc.safe.model.emg.EmgPlanPublicSummary;
import com.nbjtjc.safe.service.emg.EmgPlanPublicService;
import com.nbjtjc.safe.service.emg.EmgPlanPublicSummaryService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

/**
 * 应急总结
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/emgSummary")
public class EmgSummaryController {
    @Autowired
    private EmgPlanPublicSummaryService summaryService;

    @Autowired
    private EmgPlanPublicService emgPlanPublicService;
    @RequestMapping(value = "/delete",method = {RequestMethod.POST})
    @SysLog("删除应急总结")
    public Result delete(int id) {
        Result result=new Result();
        try {
            summaryService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/add",method = {RequestMethod.POST})
    @SysLog("添加应急总结")
    public Result add(EmgPlanPublicSummary summary) {
        Result result=new Result();

        try {
            result=summaryService.add(summary);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }
    @RequestMapping(value = "/update",method = {RequestMethod.POST})
    @SysLog("更新应急总结")
    public Result update(EmgPlanPublicSummary summary) {
        Result result=new Result();

        try {
            result=summaryService.update(summary);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/find",method = {RequestMethod.POST})
    public Result find(EmgPlanPublicSummary summary, Page page) {
        Result result=new Result();
        try {
            result=summaryService.find(summary,page);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }
    @RequestMapping(value = "/findLowerDeptSummary",method = {RequestMethod.POST})
    public Result findLowerDeptSummary(EmgPlanPublicSummary summary, BindingResult bindingResult) {
        Result result=new Result();
        try {
            result=summaryService.findLowerDeptSummary(summary);
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }

    @PostMapping("/getSummaryInitInfo")
    @ApiOperation("获取应急预案总结的初始信息，时间范围，文件编号等")
    public Result getSummaryInitInfo(int planPublicId,int startPlanPublicId){
        Result result=new Result();
        try {
            result.setData(emgPlanPublicService.getPlanPublicSummaryInitInfo(planPublicId,startPlanPublicId));
        } catch (Exception e) {
            e.printStackTrace();
            result.setFalse(e.getMessage());
        }
        return result;
    }


    /**
     * 获取当前用户是否能填写当天的工作简报
     * @param planPublicId
     * @return
     */
    @GetMapping("/getSummaryInitInfo/{planPublicId}")
    public Result getWritePermission(@PathVariable("planPublicId") int planPublicId){
        Result result=new Result();
        result.setData(summaryService.writeable(planPublicId));
        return result;
    }
}
